{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 开始使用正则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 19), match='hello,python,I love'>\n",
      "匹配成功!\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"hello,python,I love you\"\n",
    "x = re.search(\"^^hello.*love\", txt)\n",
    "print(x)\n",
    "if x:\n",
    "  print(\"匹配成功!\")\n",
    "else:\n",
    "  print(\"匹配失败\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## re.findall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['我爱', '我爱', '我爱']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"我爱python,我爱学习，我爱自己\"\n",
    "x = re.findall(\"我爱\", txt)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "匹配失败了呀！\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"菜鸟并不菜\"\n",
    "\n",
    "\n",
    "x = re.findall(\"川川\", txt)\n",
    "print(x)\n",
    "\n",
    "if (x):\n",
    "  print(\"匹配成功了哟\")\n",
    "else:\n",
    "  print(\"匹配失败了呀！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['pyworld', 'python']\n",
      "匹配成功了哟\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "x=re.findall(r'p[a-z]*', 'hello pyworld,I love python')\n",
    "\n",
    "print(x)\n",
    "\n",
    "if (x):\n",
    "  print(\"匹配成功了哟\")\n",
    "else:\n",
    "  print(\"匹配失败了呀！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('age', '20'), ('ID', '123456')]\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "# \\W 匹配任何非字母与数字字符  \\d 匹配整数  +匹配1到任意次重复\n",
    "x=re.findall(r'(\\w+)=(\\d+)', 'my age=20 and ID=123456')\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## re.search()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个空格字符位于位置: 3\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"这就是 快乐 吗？\"\n",
    "x = re.search(\"\\s\", txt)\n",
    "\n",
    "print(\"第一个空格字符位于位置:\", x.start()) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"人生苦短\"\n",
    "x = re.search(\"python\", txt)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "匹配成功：  python\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "str = 'so Love python!!'\n",
    "\n",
    "match = re.search(r'\\s\\w\\w\\w\\w\\w\\w', str)\n",
    "\n",
    "if match:\n",
    "  print('匹配成功：', match.group()) \n",
    "else:\n",
    "  print('匹配失败')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ing\n",
      "du\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "match = re.search(r'..g', 'bing') \n",
    "match2 = re.search(r'du', 'baidu')\n",
    "print(match.group())\n",
    "print(match2.group())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## re.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['人生苦短', '我爱python']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"人生苦短 我爱python\"\n",
    "x = re.split(\"\\s\", txt)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['一花', '一世界', '一生 只想你']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"一花 一世界 一生 只想你\"\n",
    "x = re.split(\"\\s\", txt, 2)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['how', 'are', 'you']\n",
      "['你好啊', '在干嘛呢']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "txt = 'how,are,you'\n",
    "txt2='你好啊，在干嘛呢'\n",
    "x=re.split(r'\\W+',txt )\n",
    "y=re.split(r'\\W+',txt2)\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## re.sub()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"hello world\"\n",
    "x = re.sub(\"h\", \"H\", txt)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world，Hello python,hello c\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"hello world，hello python,hello c\"\n",
    "x = re.sub(\"h\", \"H\", txt,2)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## re.compile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h', 'e', 'h']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "p = re.compile('[a-h]')\n",
    "\n",
    "print(p.findall(\"hello,python\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2', '2']\n",
      "['2', '0', '2', '2']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "p = re.compile('\\d')\n",
    "print(p.findall(\"my age is 22\"))\n",
    "\n",
    "print(p.findall(\"The year is 2022\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表符号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h', 'e', 'l', 'l', 'c', 'h', 'a', 'c', 'h', 'a']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"hello chuanchuan \"\n",
    "\n",
    "#按字母顺序查找“a”和“m”之间的所有小写字符\n",
    "\n",
    "x = re.findall(\"[a-m]\", txt)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2835809579@qq.com\n"
     ]
    }
   ],
   "source": [
    "str='2835809579@qq.com'\n",
    "match = re.search(r'[\\w]+@[\\w.]+', str)\n",
    "if match:\n",
    "    print(match.group())  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 星号符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"hello world\"\n",
    "x = re.findall(\"he..o\", txt)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 开始符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h']\n",
      "匹配成功\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"hello python \"\n",
    "\n",
    "\n",
    "x = re.findall(\"^h+\", txt)\n",
    "print(x)\n",
    "if x:\n",
    "    print('匹配成功')\n",
    "else:\n",
    "    print('匹配失败')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 结束符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['world']\n",
      "匹配成功\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"hello world\"\n",
    "\n",
    "#匹配字符串是否以“world”结尾\n",
    "\n",
    "x = re.findall(\"world$\", txt)\n",
    "print(x)\n",
    "if x:\n",
    "  print(\"匹配成功\")\n",
    "else:\n",
    "  print(\"匹配失败\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 星号符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['菜鸟', '菜鸟']\n",
      "匹配成功\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"菜鸟张三说自己总有一天不会是菜鸟\"\n",
    "\n",
    "x = re.findall(\"菜鸟*\", txt)\n",
    "\n",
    "print(x)\n",
    "if x:\n",
    "  print(\"匹配成功\")\n",
    "else:\n",
    "  print(\"匹配失败\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 加号符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['word']\n",
      "匹配成功!\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "txt = \"hello world,word will be better\"\n",
    "x = re.findall(\"word+\", txt)\n",
    "print(x)\n",
    "if x:\n",
    "  print(\"匹配成功!\")\n",
    "else:\n",
    "  print(\"匹配失败\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 集合符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['川川']\n",
      "匹配到了两次连续的川\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"川川爱python!\"\n",
    "\n",
    "#检查字符串是否包含“川”两个\n",
    "\n",
    "x = re.findall(\"川{2}\", txt)\n",
    "\n",
    "print(x)\n",
    "\n",
    "if x:\n",
    "  print(\"匹配到了两次连续的川\")\n",
    "else:\n",
    "  print(\"匹配失败\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 或符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['吃饭', '睡觉']\n",
      "匹配到了哦！\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"张三在吃饭或者睡觉\"\n",
    "\n",
    "\n",
    "\n",
    "x = re.findall(\"吃饭|睡觉|打游戏\", txt)\n",
    "\n",
    "print(x)\n",
    "if x:\n",
    "  print(\"匹配到了哦！\")\n",
    "else:\n",
    "  print(\"匹配失败\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 修饰符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 匹配指定开头"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['word']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"word will be better\"\n",
    "\n",
    "\n",
    "x = re.findall(\"\\Aword\", txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 匹配开头或结尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] ['学习'] ['我']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"我爱学习，学习爱我\"\n",
    "\n",
    "x = re.findall(r\"\\b爱\", txt)\n",
    "x2= re.findall(r\"\\b学习\", txt)\n",
    "x3= re.findall(r\"\\b我\", txt)\n",
    "print(x,x2,x3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python'] []\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"张三很爱学习，热爱python\"\n",
    "\n",
    "x = re.findall(r\"python\\b\", txt)\n",
    "x2 = re.findall(r\"热爱\\b\", txt)\n",
    "\n",
    "print(x,x2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 匹配中间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['菜鸟']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"我不是菜鸟他才是菜鸟\"\n",
    "\n",
    "\n",
    "x = re.findall(r\"菜鸟\\B\", txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['菜鸟']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"我不是菜鸟他才是菜鸟\"\n",
    "\n",
    "\n",
    "x = re.findall(r\"菜鸟\\B\", txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数字与非数字匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['20', '25']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"张三今年20岁，小明今年25岁\"\n",
    "\n",
    "#检查字符串是否包含任何位数（0-9的数字）\n",
    "\n",
    "x = re.findall(\"\\d+\", txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张', '三', '今', '年', '岁', '，', '小', '明', '今', '年', '岁'] ['张三今年', '岁，小明今年', '岁']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"张三今年20岁，小明今年25岁\"\n",
    "\n",
    "#匹配任何非数字符号\n",
    "\n",
    "x = re.findall(\"\\D\", txt)\n",
    "\n",
    "x2 = re.findall(\"\\D+\", txt)\n",
    "print(x,x2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 空格与非空格匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[' ', ' ']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"我 是 张三\"\n",
    "\n",
    "#匹配任何空格字符\n",
    "\n",
    "x = re.findall(\"\\s\", txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张', '三', '说', '他', '在', '边', '吃', '饭', '边', '打', '游', '戏'] ['张三说', '他在边吃饭', '边打游戏']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"张三说 他在边吃饭 边打游戏\"\n",
    "\n",
    "#匹配任意非空字符\n",
    "\n",
    "x = re.findall(\"\\S\", txt)\n",
    "x2=re.findall(\"\\S+\", txt)\n",
    "print(x,x2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数字与字母的匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张', '三', '爱', '吃', '土', '豆', '西', '红', '柿'] ['张三爱吃土豆', '西红柿']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"张三爱吃土豆 西红柿 \"\n",
    "\n",
    "#在每个单词字符（从a到z的字符，0-9的数字）返回匹配项，以及下划线_字符）：\n",
    "x = re.findall(\"\\w\", txt)\n",
    "x2=re.findall(\"\\w+\", txt)\n",
    "print(x,x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['!', \"'\", ' ', ' ', '?']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"hello!what's your name?\"\n",
    "\n",
    "#在每个非单词字符中返回匹配（不在A和Z之间的字符。“！”，“？”空白位等）：\n",
    "\n",
    "x = re.findall(\"\\W\", txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 贪婪模式与非贪婪模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<b>hello</b> <i>world</i>\n"
     ]
    }
   ],
   "source": [
    "## 通用匹配\n",
    "import re\n",
    "\n",
    "txt='<b>hello</b> <i>world</i>'\n",
    "\n",
    "x=re.match(r'.*',txt)\n",
    "print(x.group())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<b>\n"
     ]
    }
   ],
   "source": [
    "## 非贪婪匹配\n",
    "import re\n",
    "\n",
    "txt='<b>hello</b> <i>world</i>'\n",
    "\n",
    "x=re.match(r'(<.*?>)',txt)\n",
    "print(x.group())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 32), match='<div>test1</div><div>test2</div>'>\n",
      "<re.Match object; span=(0, 16), match='<div>test1</div>'>\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"<div>test1</div><div>test2</div>\"\n",
    "\n",
    "x= re.match(\"<div>.*</div>\",txt)\n",
    "y= re.match(\"<div>.*?</div>\",txt)\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 匹配范围符号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['l', 'p', 'n']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"I love python\"\n",
    "\n",
    "\n",
    "x = re.findall(\"[lpn]\", txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h', 'e', 'l', 'l', 'l', 'd']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"hello wo r l d\"\n",
    "\n",
    "x = re.findall(\"[a-n]\", txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['+']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "txt = \"5+6=11\"\n",
    "\n",
    "#检查字符串是否有任何 + 字符：\n",
    "\n",
    "x = re.findall(\"[+]\", txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对象匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(6, 8), match='wo'>\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "#search() 函数返回一个 Match 对象：\n",
    "\n",
    "txt = \"hello world\"\n",
    "x = re.search(\"wo\", txt)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6, 11)\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "#搜索单词开头的小写“w”字符，并打印其位置\n",
    "\n",
    "txt = \"hello world\"\n",
    "x = re.search(r\"\\bw\\w+\", txt)\n",
    "print(x.span())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "#返回字符串\n",
    "\n",
    "txt = \"hello world\"\n",
    "x = re.search(r\"\\bw\\w+\", txt)\n",
    "print(x.string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "world\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "#搜索单词开头的小写“w”字符，并打印该单词：\n",
    "\n",
    "txt = \"hello world\"\n",
    "x = re.search(r\"\\bw\\w+\", txt)\n",
    "print(x.group())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实战"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入爬取的页数：1\n",
      "一共有17条段子\n"
     ]
    }
   ],
   "source": [
    "# coding=utf-8\n",
    "import requests\n",
    "import re\n",
    "import logging\n",
    "\n",
    "logging.captureWarnings(True)\n",
    "import time\n",
    "\n",
    "# 第一页\n",
    "url = 'https://xiaohua.zol.com.cn/baoxiaonannv/'\n",
    "# 第n页\n",
    "url2 = 'https://xiaohua.zol.com.cn/baoxiaonannv/%d.html'\n",
    "\n",
    "header = {\n",
    "    \"User-Agent\": \"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Mobile Safari/537.36\",\n",
    "}\n",
    "\n",
    "\n",
    "def writefile(items):\n",
    "    with open('duanzi.txt', 'a') as f:\n",
    "        print('一共有%d条段子' % len(items))\n",
    "        for item in items:\n",
    "            f.write(item + '\\n')\n",
    "            f.write('————————————————————————————————————————\\n')\n",
    "        pass\n",
    "\n",
    "\n",
    "# 正则表达式\n",
    "pattern = re.compile(r'<div class=\"summary-text\">(.*?)</div>')\n",
    "\n",
    "\n",
    "def load(page):\n",
    "    if page == 1:\n",
    "        # 获取源码内容，verify=False不认证\n",
    "        response = requests.get(url, headers=header, verify=False, timeout=10).text\n",
    "        # 正则匹配\n",
    "        item = pattern.findall(response)\n",
    "        # 写入\n",
    "        writefile(item)\n",
    "    elif page > 1:\n",
    "        for p in range(1, page + 1):\n",
    "            if p == 1:\n",
    "                duanzi = url\n",
    "            else:\n",
    "                duanzi = url2 % (p)\n",
    "            print(duanzi)\n",
    "            requests.packages.urllib3.disable_warnings()\n",
    "            response = requests.get(duanzi, headers=header, verify=False, timeout=10).text\n",
    "            # 正则匹配\n",
    "            item = pattern.findall(response,re.S)\n",
    "            # 写入\n",
    "            writefile(item)\n",
    "            time.sleep(2)\n",
    "        pass\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    n = int(input('请输入爬取的页数：'))\n",
    "    load(n)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['I', 'love', 'myself']\n"
     ]
    }
   ],
   "source": [
    "# 第一题\n",
    "import re\n",
    "\n",
    "txt='I love myself'\n",
    "\n",
    "x=re.split('\\s',txt)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['blog.csdn.net']\n",
      "['blog.csdn.net']\n"
     ]
    }
   ],
   "source": [
    "#第二题\n",
    "import re\n",
    "\n",
    "txt='https://blog.csdn.net/weixin_46211269?spm=1000.2115.3001.5343'\n",
    "\n",
    "x=re.findall(r'https://([\\w.]+)/',txt)\n",
    "y=re.findall(r'https://(.*?)/',txt)#非贪婪模式\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " 职位描述 \n",
      "工作内容：\n",
      " 1. Web 应用开发\n",
      " 2. PaaS云平台开发\n",
      " 3. 系统维护、调试\n",
      " 热烈欢迎各位 计算机相关专业  或非计算机相关专业 的热爱技术，有志于从事互联网产品开发的同学投递该职位。\n",
      " 先决条件：\n",
      " 1. 请确保你至少对 Python、JavaScript 这两门语言都有深入学习研究的欲望\n",
      " 2. 对 Web 前端的知识及其实现充满好奇，能够对所有技术保持好奇更好 3. 技术是为产品服务的，必须要掌握或愿意学习掌握将技术转化为产品的能力 对于以上三点有不同看法的，不适合从事该职位。\n",
      " \n",
      "\n"
     ]
    }
   ],
   "source": [
    "#第三题\n",
    "import re\n",
    "\n",
    "txt='''\n",
    "<h3>职位描述</h3>\n",
    "工作内容：\n",
    "<br>1. Web 应用开发\n",
    "<br>2. PaaS云平台开发\n",
    "<br>3. 系统维护、调试\n",
    "<br>热烈欢迎各位 计算机相关专业  或非计算机相关专业 的热爱技术，有志于从事互联网产品开发的同学投递该职位。\n",
    "<br>先决条件：\n",
    "<br>1. 请确保你至少对 Python、JavaScript 这两门语言都有深入学习研究的欲望\n",
    "<br>2. 对 Web 前端的知识及其实现充满好奇，能够对所有技术保持好奇更好<br>3. 技术是为产品服务的，必须要掌握或愿意学习掌握将技术转化为产品的能力<br>对于以上三点有不同看法的，不适合从事该职位。\n",
    "</div>\n",
    "'''\n",
    "\n",
    "x=re.sub(r'</?\\w+>',' ',txt)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['283480957@qq.com']\n",
      "['283480957@qq.com']\n"
     ]
    }
   ],
   "source": [
    "#第四题\n",
    "import re\n",
    "\n",
    "txt='email:283480957@qq.com'\n",
    "\n",
    "x=re.findall(r'\\d+@.*com',txt)\n",
    "y=re.findall(r':(.*)',txt) #使用贪婪模式\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "15682448658\n"
     ]
    }
   ],
   "source": [
    "#第五题\n",
    "#不使用正则\n",
    "import re\n",
    "\n",
    "def Phone(str):\n",
    "    if len(str) != 11:\n",
    "        return False\n",
    "    if str[0] != '1':\n",
    "        return False\n",
    "    #匹配39 58\n",
    "    if str[1:3] !='39' and str[1:3] !='58':\n",
    "        return False\n",
    "    for i in range(3,11):\n",
    "        if str[i] < '0' or str[i] > '9':\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "print(Phone('13990909090'))\n",
    "\n",
    "\n",
    "# 使用正则匹配手机号\n",
    "def checkMobile2(str):\n",
    "    preg = '^1(([3579]\\d)|([47]\\d))\\d{8}$'\n",
    "   \n",
    "    res = re.match(preg, str)\n",
    "    return res\n",
    "\n",
    "\n",
    "print(checkMobile2('15682448658').group())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "匹配成功!\n",
      "匹配失败!\n"
     ]
    }
   ],
   "source": [
    "#第六题\n",
    "import re\n",
    "def p(text):\n",
    "        patterns = '\\w*z.\\w*'\n",
    "        if re.search(patterns,  text):\n",
    "                return '匹配成功!'\n",
    "        else:\n",
    "                return('匹配失败!')\n",
    "\n",
    "print(p(\"how are uou zip.\"))\n",
    "print(p(\"Python Exercises.\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "csdn\n",
      "zhihu\n",
      "github\n",
      "stackoverflow\n"
     ]
    }
   ],
   "source": [
    "#第七题\n",
    "import re\n",
    "items = [\"csdn (.com)\", \"zhihu(.com)\", \"github (.com)\", \"stackoverflow (.com)\"]\n",
    "for item in items:\n",
    "    print(re.sub(r\" ?\\([^)]+\\)\", \"\", item))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原来如期为:  2022-09-12\n",
      "修改后日期为： 12-09-2022\n"
     ]
    }
   ],
   "source": [
    "#第八题\n",
    "import re\n",
    "def change(dt):\n",
    "        return re.sub(r'(\\d+)-(\\d+)-(\\d+)', '\\\\3-\\\\2-\\\\1', dt)\n",
    "dt = \"2022-09-12\"\n",
    "print(\"原来如期为: \",dt)\n",
    "print(\"修改后日期为：\",change(dt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
